package cn.mw.cmdb.entity;

import cn.mw.cmdb.param.ImportContextParam;
import cn.mw.components.mongodb.entity.CriteriaOpsType;
import cn.mw.microMonitorCommon.entity.cmdb.ModelExportDataInfoParam;
import cn.mw.microMonitorCommon.entity.cmdb.MwImportResultParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.mongodb.core.query.Criteria;

import java.util.*;

import static cn.mw.cmdb.service.impl.InstanceViewImpl.*;
import static cn.mw.cmdb.util.ValConvertUtil.booleanValueConvert;

@Slf4j
public class PropertyTypeSwitch extends PropertyType<PropertyTypeSwitch, Boolean> {

    public static final String ID = "switch";
    public static final String trueKey = "TRUE";
    public static final String falseKey = "FALSE";


    public PropertyTypeSwitch() {
        setId(ID);
        setName("开关型");
        setValueTip("");
        setSortNum(17);
    }

    @Override
    public boolean validateValue(Object value, PropertyTypeRef propertyTypeRef) throws Exception {
        if (value instanceof String) {
            Boolean.parseBoolean(value.toString());
        }
        return true;
    }

    @Override
    public Boolean convertValue(Object value) {
        if (value instanceof String) {
            return Boolean.parseBoolean(value.toString());
        }
        if (value instanceof Integer) {
            return (Integer) value == 1;
        }
        return (Boolean) value;
    }

    @Override
    public Criteria doCriteria(CriteriaOpsType opsType, String name, Object value) {
        Criteria criteria = null;
        switch (opsType) {
            case is:
                criteria = new Criteria(name).is(value);
                break;
            case exist:
                criteria = new Criteria(name).exists((boolean) value);
                break;
            case ne:
                criteria = new Criteria(name).ne(value);
        }
        return criteria;
    }

    @Override
    public CriteriaOpsType getDefaultCriteriaOpsType() {
        return CriteriaOpsType.is;
    }

    @Override
    public PropertyTypeSwitch newObj() {
        return new PropertyTypeSwitch();
    }

    @Override
    public Object readValue(Object val, Map<String, Object> context) {
        if (val != null && val instanceof Boolean) {
            boolean jsonBool = booleanValueConvert(val);
            return jsonBool;
        }
        return null;
    }

    @Override
    public void refresh() {

    }

    @Override
    public boolean tableHeaderInfo(Map ms, PropertyInfo propertyInfo, List<String> labelNames, List<String> labelId) throws Exception {
        //导出表头数据时，表头字段的id和名称不能同时重复
        if ((labelId != null && !labelId.contains(propertyInfo.getId())) && (labelNames != null && !labelNames.contains(ms.get(nameKey)))) {
            ms.put(typeKey, PropertyTypeExtSingle.ID);
            Set<String> instanceNames = new HashSet<>();
            ms.put(selectDataKey, Arrays.asList(trueKey, falseKey));
            labelId.add(propertyInfo.getId());
            return true;
        }
        return false;
    }

    @Override
    public Object getDataValue(ImportContextParam contextParam) {
        MwImportResultParam resultParam = new MwImportResultParam();
        String message = "";
        ModelExportDataInfoParam importParam = contextParam.getParam();
        //属性名称
        String tableName = importParam.getTableName();
        Object cellValue = importParam.getCellValue();
        Map<String, Object> matchMap = new HashMap<>();
        importCheckInfo(resultParam, contextParam, matchMap);
        log.info("波尔类型::");
        resultParam.setCellValue(booleanValueConvert(cellValue));
        return resultParam;
    }
}
